﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using PlayDeezGames.Common.Names;

namespace PlayDeezGames.Common
{
    public class StringListProperty : Property
    {
        public class StringListPropertyPropertyFactory : IPropertyFactory
        {
            public object LoadFromXElement(System.Xml.Linq.XElement theXElement)
            {
                List<string> result = new List<string>();
                foreach (var entry in theXElement.Elements(Names.CommonXNames.Entry))
                {
                    result.Add(entry.Value);
                }
                return result;
            }

            public void SaveToXElement(System.Xml.Linq.XElement theXElement, object theObject)
            {
                List<string> list = theObject as List<string>;
                foreach (var value in list)
                {
                    theXElement.Add(new XElement(Names.CommonXNames.Entry,value));
                }
            }
            private StringListPropertyPropertyFactory()
            {
            }
            public static void Register()
            {
                PropertyFactories.RegisterFactory(Names.CommonTypes.StringList, new StringListProperty.StringListPropertyPropertyFactory(),Names.CommonFactories.StringListPropertyFactory);
            }
            public static void Unregister()
            {
                PropertyFactories.UnregisterFactory(Names.CommonTypes.StringList);
            }


            public object CreateNew()
            {
                return new StringGenerator();
            }
        }

        public class StringListPropertyFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, XElement theXElement)
            {
                return new StringListProperty(theParent as Descriptor, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new StringListProperty(theParent as Descriptor);
            }
            private StringListPropertyFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.StringListPropertyFactory, new StringListProperty.StringListPropertyFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.StringListPropertyFactory);
            }
        }

        protected StringListProperty(Descriptor parent)
            : base(parent)
        {
            Type = CommonTypes.StringList;
        }
        protected StringListProperty(Descriptor parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = base.OnToXElement();
            result.Attribute(PlayDeezGames.Common.Names.CommonXNames.Factory).Value = Names.CommonFactories.StringGeneratorPropertyFactory;
            return result;
        }

        public List<string> Value
        {
            get
            {
                return Payload as List<string>;
            }
        }

        public static new StringListProperty Create(Descriptor theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.StringListPropertyFactory) as StringListProperty;
        }
        public static new StringListProperty Load(Descriptor theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as StringListProperty;
        }
    }
}
